package com.amado.course27.service.impl;

import com.amado.course27.dao.AllDao;
import com.amado.course27.entity.Goods;
import com.amado.course27.entity.GoodsCate;
import com.amado.course27.service.GoodsCateService;

/**
 * 商品分类业务逻辑
 * 
 * @author wangyulong
 * @date 2018-01-01
 *
 */
public class GoodsCateServiceImpl implements GoodsCateService {
	private GoodsCate[] cateArray = new GoodsCate[100];
	private int goodsCateCount= 0;
	@Override
	public boolean addcate(GoodsCate cate) {
		if (checkParam(cate)) {
			cateArray[goodsCateCount++] = cate;
			return true;
		}
		return false;
	}

	boolean checkParam(GoodsCate cate) {
		if (cate == null) {
			System.out.println("非法入参");
			return false;
		}
		if (cate.getCateCode() == null) {
			return false;
		}
		if (cate.getCateName() == null||cate.getCateName().equals("")) {
			return false;
		}
		return true;

	}

	@Override
	public void updatecate(GoodsCate cate) {
		// TODO Auto-generated method stub

	}

	@Override
	public boolean deletecate(String cateCode) {
		//判断是否存在
		for(GoodsCate cate:cateArray){
			if(cate!=null){
				if(cate.getCateCode().equals(cateCode)){
					if(isExistsChild(cateCode)){
						System.out.println("有子节点，删除失败，请先删除子节点");
						return false;
					}else{
						//删除
						return true;
					}
				}
			}
		}
		//是否在子节点
		return false;
	}
	private boolean isExistsChild(String cateCode) {
		for(GoodsCate cate:cateArray){
			if(cate!=null){
				if(cate.getCateCode().equals(cateCode)){
					return true;
				}
			}
		}
		return false;
	}
	@Override
	public GoodsCate[] listcate() {
		return cateArray;
	}

	@Override
	public boolean isExists(String cateCode) {
		for(GoodsCate cate:cateArray){
			if(cate!=null){
				if(cate.getCateCode().equals(cateCode)){
					return true;
				}
			}
		}
		return false;
	}

}
